<template>
  <div class="settings-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">Your Settings</h1>
          <form @submit.prevent="onSubmit">
            <fieldset>
              <fieldset class="form-group">
                <input
                  class="form-control"
                  type="text"
                  placeholder="URL of profile picture"
                  v-model="user.image"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  class="form-control form-control-lg"
                  type="text"
                  placeholder="Your Name"
                  v-model="user.username"
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  class="form-control form-control-lg"
                  rows="8"
                  placeholder="Short bio about you"
                  v-model="user.bio"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  class="form-control form-control-lg"
                  type="text"
                  placeholder="Email"
                  v-model="user.email"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  class="form-control form-control-lg"
                  type="password"
                  placeholder="Password"
                  v-model="user.password"
                />
              </fieldset>
              <button class="btn btn-outline-danger" @click.prevent="logout">
                click hear to logout
              </button>
              <button class="btn btn-primary pull-xs-right">
                Update Settings
              </button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>
<style>
</style>
<script>
// import {mapState} from 'vuex'
import {updataUser,getUser} from '@/api/user'
const Cookie = process.client ? require('js-cookie') : undefined

export default {
  middleware:'auth',
  name:"SettingsIndex",
  // data(){
  //   return {
  //     user:{
  //       username:'',
  //       image:'',
  //       bio:'',
  //       email:'',
  //       password:'',
  //     }
  //   }
  // },
  async asyncData(){
    const {data} = await getUser()
    return data
  },
  // computed:{
  //   ...mapState(['user'])
  // },
  methods:{
    async onSubmit(){
      console.log(this.user)
      try{
        const {data} = await updataUser({user:this.user})
        // 保存用户信息
        this.$store.commit('setUser',data.user)

        // 数据持久化
        Cookie.set('user',data.user)

        this.$router.push({
          name: 'profile',
          params: {
            username: data.user.username,
          },
        })
      }catch(e){
        console.log(e.response.data.errors)
      }
    },
    logout(){
      // console.log(123)
      // 清空用户信息
      this.$store.commit('setUser',null)

      // 数据持久化
      Cookie.set('user',null)

      this.$router.push("/")
    }
  }
};
</script>